package com.itheima.web.service.impl.ums;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.result.ResultData;
import com.itheima.service.ums.AdminService;

import com.itheima.service.ums.RoleService;
import com.itheima.service.ums.UmsMenuService;
import com.itheima.utils.BCryptUtil;
import com.itheima.utils.JwtUtils;
import com.itheima.vo.ums.AdminRole;
import com.itheima.vo.ums.UmsAdmin;
import com.itheima.vo.ums.UmsMenu;
import com.itheima.vo.ums.UmsRole;
import com.itheima.web.mapper.ums.AdminMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class AdminServiceImpl extends ServiceImpl<AdminMapper, UmsAdmin> implements AdminService{
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private UmsMenuService umsMenuService;
    @Autowired
    private RoleService roleService;

    /**
     * 用户登录
     * @param umsAdmin
     * @return
     */
    @Override
    public String login(UmsAdmin umsAdmin) {
        LambdaQueryWrapper<UmsAdmin> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UmsAdmin::getUsername,umsAdmin.getUsername());
        //lqw.eq(UmsAdmin::getPassword,umsAdmin.getPassword());
        UmsAdmin u = this.getOne(lqw);
        if(u == null){
            return null;
        }
        String upassword = u.getPassword();
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        boolean matches = bCryptPasswordEncoder.matches(umsAdmin.getPassword(), upassword);

        if(!matches){
            return null;
        }

        Map retMap = new HashMap();
        Map tokenMap = new HashMap<>();

        tokenMap.put("id",u.getId());

        String token = JwtUtils.getToken(tokenMap);

        retMap.put("token",token);
        return token;
    }

    /**
     * 获取用户权限
     * @return
     */
    @Override
    public HashMap<String, Object> info() {
        String username = "admin";
        HashMap<String, Object> map = new HashMap<>();
        LambdaQueryWrapper<UmsAdmin> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UmsAdmin::getUsername,username);
        UmsAdmin user = this.getOne(lqw);

        map.put("icon",user.getIcon());
        map.put("username",user.getUsername());

        List<UmsRole> roles = adminMapper.getRoles(user.getId());
        List<String> list = new ArrayList<>();
        for (UmsRole role : roles) {
            list.add(role.getName());
        }
        map.put("roles",list);

        List<UmsMenu> menus = adminMapper.getMenus(user.getId());

        map.put("menus",menus);

        return map;
    }

    /**
     * 分页查询用户列表
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    @Override
    public Page<UmsAdmin> getPage(Integer pageNum, Integer pageSize, String keyword) {
        Page<UmsAdmin> umsAdminPage = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<UmsAdmin> umsadminLQW = new LambdaQueryWrapper<>();
        umsadminLQW.like(keyword != null,UmsAdmin::getUsername,keyword).
                or().
                like(keyword != null,UmsAdmin::getNickName,keyword);

        this.page(umsAdminPage,umsadminLQW);

        return umsAdminPage;
    }
    /**
     * 用户状态禁用，启用
     * @param id
     * @param status
     * @return
     */
    @Override
    public void updateStatus(Integer id, Integer status) {
        UmsAdmin admin = this.getById(id);
        admin.setStatus(status);

        //修改状态为status=0,禁用,status=1,启用
        this.updateById(admin);
    }

    /**
     * 权限存储
     * @param adminRole
     */
    @Override
    public void saveRole(AdminRole adminRole) {
        adminMapper.saveRole(adminRole);
    }

}
